<?

// DATA SAVE HANDLERS
global $firstField;
$firstField = null;

function stdform_save($p)
{
  return($_REQUEST[$p['name']]);
}

function stdform_string_save($p)
{
  return(stdform_save($p));
}

function stdform_hidden_save($p)
{
  return(stdform_save($p));
}

function stdform_readonly_save($p)
{
  return(stdform_save($p));
}

function stdform_dropdown_save($p)
{
  return(stdform_save($p));
}

function stdform_password_save($p)
{
  return(stdform_save($p));
}

function stdform_text_save($p)
{
  return(stdform_save($p));
}

// DISPLAY CODE
function stdform_documents($p, &$form)
{
  $id = $form->params[$form->defaultIdFieldname];
  $id = str_repeat('0', 4-strlen($id)).$id;
  $p['context'] = getDefault($p['context'], $form->tableName.'/'.$id);
  $p['context'] = getDefault($p['context'], 'default');
  if ($p['caption'] != '') print($p['caption']);
  @mkdir('resource/'.$form->tableName, 0777);
  ?> 
  <iframe src="lib/predef/stdform-documents-iframe.php?ctx=<?= urlencode($p['context']) ?>" 
    style="width: 100%; height: 220px; border: 0px;">
  </iframe>
  <?
}

function stdform_handle_error(&$p)
{
  if ($p['error'] != '') 
  { 
    $p['class'] .= ' fielderror'; 
    print(' <span class="errormsg">'.$p['error'].'</span><br/>'); 
  }
}

function stdform_readonly($p)
{
  if ($p['caption'] != '') print($p['caption']);
  stdform_handle_error($p);
  ?> 
  <input type="text" readonly name="<?= $p['name'] ?>" value="<?= htmlentities(getDefault($p['value'])) ?>"/><?
}

function stdform_string($p)
{
  global $firstField;
  if ($p['caption'] != '') print($p['caption']);
  stdform_handle_error($p);
  ?> 
  <input type="text" class="<?= $p['class'] ?>" style="<?= $p['style'] ?>" id="fld_<?= $p['name'] ?>"
    name="<?= $p['name'] ?>" value="<?= htmlentities(getDefault($p['value'])) ?>"/><?
  if ($firstField == null && $p['focus'] === true)
  {
    $firstField = $p;
    ?>
    <script>
    document.getElementById('fld_<?= $p['name'] ?>').focus();
    </script>
    <?
  }
}

function stdform_dropdown($p)
{
  if ($p['caption'] != '') print($p['caption']);
  stdform_handle_error($p);
  ?> 
  <select class="<?= $p['class'] ?>" style="<?= $p['style'] ?>" name="<?= $p['name'] ?>">
  <?
  foreach ($p['options'] as $k => $v)
  {
    $selected = '';
    if ($k == $p['value']) $selected = 'selected';
    print('<option '.$selected.' value="'.htmlentities($k).'">'.htmlentities($v).'</option>');
  }
  ?>  
  </select>
  <?
}

function stdform_hidden($p)
{
  ?> 
  <input type="hidden" name="<?= $p['name'] ?>" value="<?= htmlentities(getDefault($p['value'])) ?>"/><?
}

function stdform_text($p)
{
  if ($p['caption'] != '') print($p['caption']);
  stdform_handle_error($p);
  ?> 
  <textarea class="<?= $p['class'] ?>" style="<?= $p['style'] ?>" id="fld_<?= $p['name'] ?>"
    name="<?= $p['name'] ?>"><?= htmlentities(getDefault($p['value'])) ?></textarea><?
  if ($firstField == null && $p['focus'] === true)
  {
    $firstField = $p;
    ?>
    <script>
    document.getElementById('fld_<?= $p['name'] ?>').focus();
    </script>
    <?
  }
}

function stdform_password($p)
{
  if ($p['caption'] != '') print($p['caption']);
  stdform_handle_error($p);
  ?> 
  <input class="<?= $p['class'] ?>" type="password" name="<?= $p['name'] ?>" value="<?= htmlentities(getDefault($p['value'])) ?>"/><?
}

function stdform_start($p)
{
  ?><form action="?" method="post"><?
}

function stdform_submit($p)
{
  ?><input type="submit" name="submitbtn" value="<?= $p['pure-caption'] ?>"/><?
}

function stdform_end($p)
{
  foreach ($p['params'] as $k => $v)
    print('<input type="hidden" name="'.$k.'" value="'.htmlentities($v).'"/>');
  ?></form><?
}

?>
